<template>
  <div class="achievement" v-cloak>
    <div class="bg_color"></div>
    <div style="height: 4px;position: absolute;z-index: 2222; width: 100%; top: 46px;background: #f0f0f0"></div>
    <div style="height: 50px;"></div>
    <div class="about-us" v-show="show">
      <mt-loadmore :distanceIndex="4" @top-status-change="handleTopChange" :topDistance="30" :bottomDistance='30' :maxDistance="40" :top-method="loadTop" :bottom-method="loadBottom" :bottom-all-loaded="bottomAllLoaded" ref="loadmore" :autoFill="false" :bottomAllLoaded = bottomsta>
        <div slot="top" class="mint-loadmore-top">
          <span v-show="topStatus === 'loading'" class="color_top">Loading...</span>
        </div>
        <div class="content" v-if="messageSta">
          <ul class="jyxx">
            <li @click="messdetail(item.ne_id)" v-for="(item,index) in news_list">
              <p>{{item.ne_title}}</p>
              <span>{{item.ne_add_time}}</span>
              <span @click.stop="deletemessage(item.ne_id,index)" style="position: absolute;right: 0px;bottom: 7px;z-index:0">
              <img src="../assets/image/dzb_02.png" style="width:15px;height: 17px;"/>
            </span>
              <i class="new" v-if="item.ne_status == '未读'"></i>
              <div class="clearfix"></div>
            </li>
          </ul>
        </div>
        <div v-if="!messageSta" class="nomessage">
           暂无消息
        </div>
        <div slot="bottom" class="mint-loadmore-bottom">
          <load-more tip="正在加载" v-show="loadingBottomShow"></load-more>
        </div>
      </mt-loadmore>
      <div class="delFix" @click="hideFix" v-if="isDel">
        <div class="fixBox">
          <p>确定要删除吗？</p>
          <div class="cancel btn" @click="hideFix" style="line-height:22px;float: left;margin-left: 20px;">取消</div>
          <div class="confirm btn" @click.stop="del" style="line-height:22px;">确定</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import { LoadMore, Swiper } from 'vux'
  import { Loadmore } from 'mint-ui'
  import Vue from 'vue'
  //  let $ = require('Jquery')
  import { mapState } from 'vuex'
  Vue.component(Loadmore.name, Loadmore)
  export default {
    components: {
      LoadMore,
      Swiper
    },
    name: 'MessageCenter',
    data () {
      return {
        show: true,
        isDel: false,
        limit: 10,
        end_id: 0,
        news_list: [],
        refresh: true,
        messageno: false,
        ne_id: '',
        curr_index: '',
        loadingBottomShow: false,
        topStatus: 'loading',
        bottomsta: false,
        bottomAllLoaded: false,
        research: true,
        messageSta: true
      }
    },
    computed: mapState({
      headerLeft: state => state.headerLeft
    }),
    methods: {
      loadBottom () {
        this.loadingBottomShow = true
        if (this.research === true) {
          this.messageAjax('')
        } else {
          this.loadingBottomShow = false
          this.$refs.loadmore.onBottomLoaded()
        }
      },
      handleTopChange (status) {
        this.topStatus = status
      },
      loadTop () {
        this.messageSta = true
        this.research = true
        this.end_id = 0
        this.bottomsta = false
        this.messageAjax('reflsh')
      },
      deletemessage: function (neid, index) {
        this.isDel = true
        this.ne_id = neid
        this.curr_index = index
      },
      hideFix: function () {
        this.isDel = false
      },
      del: function () {
        let thisObj = this
        thisObj.$chaos.ajax({
          data: {
            ne_id: thisObj.ne_id
          },
          slient: true,
          userinfo: true,
          url: 'User/deleteNew',
          callback: function (type, res) {
            if (res.status === 'SUCCESS') {
              thisObj.$vux.toast.show({
                text: res.result.msg,
                type: 'text',
                width: '180px',
                position: 'bottom'
              })
              thisObj.news_list.splice(thisObj.curr_index, 1)
              thisObj.isDel = false
            } else {
              thisObj.show = true
              thisObj.$vux.toast.show({
                text: res.result.msg,
                type: 'text',
                width: '180px',
                position: 'bottom'
              })
            }
          }
        })
      },
      messdetail: function (neid) {
        window.location.href = '#/MessageDetail?ne_id=' + neid
      },
      messageAjax: function (title) {
        let thisObj = this
        thisObj.clickSta = false
        thisObj.$chaos.ajax({
          data: {
            end_id: thisObj.end_id,
            limit: thisObj.limit
          },
          slient: true,
          userinfo: true,
          url: 'User/getNewsList',
          callback: function (type, res) {
            if (res.status === 'SUCCESS') {
              if (title === 'reflsh') {
                thisObj.news_list = []
                thisObj.$refs.loadmore.onTopLoaded()
              }
              thisObj.loadingBottomShow = false
              thisObj.$refs.loadmore.onBottomLoaded()
              if (res.result.news_list.length === 0) {
                thisObj.research = false
                if (thisObj.end_id === 0) {
                  thisObj.messageSta = false
                }
                return false
              }
              if (res.result.news_list < thisObj.limit) {
                thisObj.bottomsta = true
                thisObj.research = false
              } else {
                thisObj.end_id = res.result.end_id
              }
              if (res.result.news_list.length > 0) {
                thisObj.news_list = thisObj.news_list.concat(res.result.news_list)
              }
            } else {
              thisObj.show = true
              thisObj.$vux.toast.show({
                text: res.result.msg,
                type: 'text',
                width: '180px',
                position: 'bottom'
              })
            }
          }
        })
      },
      verify: function () {
        this.$chaos.setTitle('消息中心')
        this.$store.commit('headerShow', true)
        this.$store.commit('title', '消息中心')
        this.$store.commit('footerShow', false)
        this.messageAjax('')
      }
    },
    created: function () {
      this.verify()
    },
    mounted: function () {
      window.scrollTo(0, 2000)
    },
    watch: {
      '$route': 'verify'
    }
  }
</script>
<style scoped>
  .mint-loadmore-top{
    text-align: center;
    line-height: 33px;
  }
  [v-cloak] {
    display: block;
  }
  body,
  html {
    background: #f9f9f9;
  }
  .con_term{
    padding: 0px 15px;
  }
  .delFix {
    width: 100%;
    height: 100%;
    position: fixed;
    background: rgba(0,0,0,0.3);
    left: 0;
    top: 0;
    z-index: 1;
  }
  .delFix .fixBox {
    width: 200px;
    height: 70px;
    margin: 250px auto 0;
    background: #fff;
    border-radius: 10px;
    position: relative;
    padding: 30px 0 15px;
  }
  .delFix .fixBox > p {
    text-align: center;
    height: 20px;
    line-height: 20px;
    margin-bottom: 20px;
  }
  delFix .fixBox .cancel {
    float: left;
    margin-left: 23px;
    color: rgb(255,160,40);
  }
  .delFix .fixBox .btn {
    width: 65px;
    height: 25px;
    border: 1px solid rgb(255,160,40);
    border-radius: 5px;
    text-align: center;
  }
  .delFix .fixBox .confirm {
    float: right;
    margin-right: 23px;
    color: #fff;
    background: rgb(255,160,40);
  }
  .delFix .fixBox .btn {
    width: 65px;
    height: 25px;
    border: 1px solid rgb(255,160,40);
    border-radius: 5px;
    text-align: center;
  }
  .content .jyxx {
    background: #fff;
  }
  .content .jyxx li {
    border-bottom: 6px solid #fcfcfc;
    position: relative;
    padding: 10px 0 10px;
  }
  .content .jyxx li p {
    height: 40px;
    line-height: 20px;
    padding: 0px 20px;
    overflow: hidden;
    font-size: 14px;
  }
  .content .jyxx li p {
    height: 40px;
    line-height: 20px;
    padding: 0px 20px;
    overflow: hidden;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .content .jyxx li span:nth-child(2) {
    color: #777;
    font-size: 14px;
    float: left;
    padding: 0px 20px;
    margin-top: 10px;
  }
  .content .jyxx li span:nth-child(3) {
    float: right;
    padding: 0px 20px;
    margin-top: 10px;
  }
  img {
    width: 100%;
    height: 100%;
  }
  .content .jyxx li .new {
    position: absolute;
    background: rgb(245, 38, 116);
    width: 5px;
    height: 5px;
    border-radius: 50%;
    top: 38px;
    left: 10px;
  }
  .clearfix {
    clear: both;
  }
  .nomessage{
    text-align: center;
    margin-top: 20px;
    color: #999;
  }
</style>
